package com.bjdd.jdy.ui.hotMap;

import com.baidu.location.BDLocation;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.baidu.LocationUtils;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;

import rx.Observable;

/**
 * Created by Administrator on 2017/4/19.
 */

public class HotMapPresenter extends HotMapContract.Presenter {
    private Observable observable;
    @Override
    public void onStart() {
        if (SpUtil.getUser() == null || SpUtil.getUser().equals("") || SpUtil.getUser().getCarrierId() == null) {
            JumpUtil.toLogin(context);
            return;
        }
        observable = rxManager.on1(Constant.EVENT_LOCATION_ONCE, arg -> view.setCenter((BDLocation)arg));
        LocationUtils.getInstance(context).startOnce();
    }

    public void unregister(){
        rxManager.unregister(Constant.EVENT_LOCATION_ONCE,observable);
    }

    @Override
    void getHotMap(String cityName, int day) {
        showLoadingDialog(false);
        String token = SpUtil.getToken();
        rxManager.add(model.getHotMap(token,cityName,day)
        .subscribe(result -> {
            hideLoadingDialog();
            if (!result.isError()){
                view.success(result.getData());
            }else {
                T.show(result.getMsg());
            }
        },e -> {
            hideLoadingDialog();
            L.d("获取热力图数据失败："+e.getMessage());
            T.show("获取数据失败:"+e.getMessage());
        }));
    }
}
